#include "CylinderSegmentationModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.cylinder_segmentation_model.json";
}

CylinderSegmentationModel::CylinderSegmentationModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/cylinder_segmentation.html");
}

CylinderSegmentationModel& CylinderSegmentationModel::get()
{
    static CylinderSegmentationModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void CylinderSegmentationModel::fromJson(const QJsonObject& json, QStringList& errorList)
{
    BasePclModel::fromJson(json, errorList);

    pcdFile_ = json["pcdFile"].toString(pcdFile_);
    filterFieldName_ = json["filterFieldName"].toString(filterFieldName_);
    filterLimitsMin_ = json["filterLimitsMin"].toDouble(filterLimitsMin_);
    filterLimitsMax_ = json["filterLimitsMax"].toDouble(filterLimitsMax_);
    neKSearch_ = json["neKSearch"].toInt(neKSearch_);
    planeNormalDistanceWeight_ = json["planeNormalDistanceWeight"].toDouble(planeNormalDistanceWeight_);
    planeMaxIterations_ = json["planeMaxIterations"].toInt(planeMaxIterations_);
    planeDistanceThreshold_ = json["planeDistanceThreshold"].toDouble(planeDistanceThreshold_);

    cylinderNormalDistanceWeight_ = json["cylinderNormalDistanceWeight"].toDouble(cylinderNormalDistanceWeight_);
    cylinderMaxIterations_ = json["cylinderMaxIterations"].toInt(cylinderMaxIterations_);
    cylinderDistanceThreshold_ = json["cylinderDistanceThreshold"].toDouble(cylinderDistanceThreshold_);
    cylinderRadiusMin_ = json["cylinderRadiusMin"].toDouble(cylinderRadiusMin_);
    cylinderRadiusMax_ = json["cylinderRadiusMax"].toDouble(cylinderRadiusMax_);
}

QJsonObject CylinderSegmentationModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json.insert("pcdFile", QJsonValue(pcdFile_));
    json.insert("filterFieldName", QJsonValue(filterFieldName_));
    json.insert("filterLimitsMin", QJsonValue(filterLimitsMin_));
    json.insert("filterLimitsMax", QJsonValue(filterLimitsMax_));
    json.insert("neKSearch", QJsonValue(neKSearch_));
    json.insert("planeNormalDistanceWeight", QJsonValue(planeNormalDistanceWeight_));
    json.insert("planeMaxIterations", QJsonValue(planeMaxIterations_));
    json.insert("planeDistanceThreshold", QJsonValue(planeDistanceThreshold_));

    json.insert("cylinderNormalDistanceWeight", QJsonValue(cylinderNormalDistanceWeight_));
    json.insert("cylinderMaxIterations", QJsonValue(cylinderMaxIterations_));
    json.insert("cylinderDistanceThreshold", QJsonValue(cylinderDistanceThreshold_));
    json.insert("cylinderRadiusMin", QJsonValue(cylinderRadiusMin_));
    json.insert("cylinderRadiusMax", QJsonValue(cylinderRadiusMax_));

    return json;
}
